const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return `${[year, month, day].map(formatNumber).join('-')} ${[hour, minute, second].map(formatNumber).join(':')}`
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : `0${n}`
}


//Wed Aug 17 2022 09:54:48 GMT+0800 (中国标准时间) 转成 yyyy-MM-dd hh:mm:ss
function getformattime(timeStr) {
  var date = new Date(timeStr);
  var y = date.getFullYear();
  var m = date.getMonth() + 1;
  m = m < 10 ? ('0' + m) : m;
  var d = date.getDate();
  d = d < 10 ? ('0' + d) : d;
  var h = date.getHours();
  h = h < 10 ? ('0' + h) : h;
  var min = date.getMinutes();
  min = min < 10 ? ('0' + min) : min;
  var s = date.getSeconds();
  s = s < 10 ? ('0' + s) : s;
  return y + '-' + m + '-' + d + ' ' + h + ':' + min + ':' + s;
}


/**
 * 对字符串判空
 */
function isStrEmpty(string) {
  if (string == null || string == undefined || string == '' || string.replace(/\s+/g, '').length == 0 || string == "undefined") {
    return true;
  }
  return false;
}

/**
 * 将yyyy-MM-dd HH:mm:ss 切成 HH:mm
 */
function splitTimeStr1(str) {
  var time = str.split(' ');
  var result = time[1].split(':');
  var resultStr = result[0] + ':' + result[1];
  return resultStr;
}

/**
 * 通过出生年月日得到年龄
 * @param {*} birth 
 * @returns 
 */
function countAgeByBirth(birth) {
  const today = new Date(); // 当前日期
  const birthdate = new Date(birth); // 出生日期
  let age = today.getFullYear() - birthdate.getFullYear(); // 计算年龄
  const monthDiff = today.getMonth() - birthdate.getMonth(); // 计算月份差
  if (monthDiff < 0 || (monthDiff === 0 && today.getDate() < birthdate.getDate())) {
    age--; // 如果月份差为负数或者月份差为零但是当前日期小于出生日期的日期，则年龄减一
  }
  return age;
}

/**
 * 获取后端返回的文件流，进行下载，在app下载处用到
 * @param {*} result 
 */
function exportFile(result) {
  let contentDisposition = result.headers['content-disposition'];
  // 这里后端给的内容中，文件名字可能是驼峰式名称的 fileName ，或者是全部小写的 filename
  let filename = decodeURI(contentDisposition.split('fileName=')[1] || contentDisposition.split('filename=')[1], "UTF-8");
  // 注意这里的 result.data ，如果只传 result 的话，最后下载出来的excel文件，里面显示的是 [object Object]
  let blob = new Blob([result.data], { type: result.headers['content-type'] });
  let url = window.URL.createObjectURL(blob);
  if (window.navigator.msSaveBlob) {  //IE
    try {
      window.navigator.msSaveBlob(blob, filename);
    }
    catch (e) {
      console.log(e);
    }
  }
  else {  //非IE
    let link = document.createElement('a');
    link.style.display = 'none';
    link.href = url;
    link.setAttribute('download', filename);
    document.body.appendChild(link);
    link.click();
  }
  URL.revokeObjectURL(url); // 释放内存
}

/**
 * base64图片转文件
 * @param {*} data 
 * @param {*} fileName 
 * @returns 
 */
function base64ToFile(data, fileName) {
  const dataArr = data.split(',')
  const byteString = atob(dataArr[1])
  const options = {
    type: 'image/jpeg',
    endings: 'native'
  }
  const u8Arr = new Uint8Array(byteString.length)
  for (let i = 0; i < byteString.length; i++) {
    u8Arr[i] = byteString.charCodeAt(i)
  }
  return new File([u8Arr], fileName + '.png', options)
}

export {
  formatTime,
  getformattime,
  isStrEmpty,
  splitTimeStr1,
  countAgeByBirth,
  exportFile,
  base64ToFile
}

